package com.org.gen.controller;

import com.org.common.core.entity.R;
import com.org.gen.entity.GeneratorParam;
import com.org.gen.entity.GenDatasourceConfig;
import com.org.gen.config.GeneratorConfig;
import com.org.gen.service.IGenDatasourceConfigService;
import com.org.gen.service.IGenGeneratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("generate")
public class GenGeneratorController {

    @Autowired
    private IGenDatasourceConfigService datasourceConfigService;

    @Autowired
    private IGenGeneratorService generatorService;

    /**
     * 生成代码
     *
     * @param generatorParam 生成参数
     * @return 返回代码内容
     */
    @RequestMapping("/code")
    public R code(@RequestBody GeneratorParam generatorParam) {
        int datasourceConfigId = generatorParam.getDatasourceConfigId();
        GenDatasourceConfig datasourceConfig = datasourceConfigService.getById(datasourceConfigId);
        GeneratorConfig generatorConfig = GeneratorConfig.build(datasourceConfig);
        return R.ok(generatorService.generate(generatorParam, generatorConfig));
    }

}
